package com.peanut.baby.mvp.follow;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.User;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/7.
 */

public class FollowFansPresenter implements FollowFansContract.Presenter{
    FollowFansContract.View view;
    private Activity activity;
    public FollowFansPresenter(FollowFansContract.View view,Activity activity){
        this.view = view;
        this.activity = activity;
    }
    @Override
    public void getFollowList(String userId,int pageNo, int pageSize) {
        Observable obs = RetrofitClient.getInstance().getUserFollowings(userId,pageNo,pageSize, InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<List<User>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<User>>() {
            @Override
            protected void onHandleSuccess(List<User> o) {
                view.onListGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onRequestFailed(msg);
            }

        });
    }

    @Override
    public void getFansList(String userId,int pageNo, int pageSize) {
        Observable obs = RetrofitClient.getInstance().getUserFans(userId,pageNo,pageSize, InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<List<User>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<User>>() {
            @Override
            protected void onHandleSuccess(List<User> o) {
                view.onListGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onRequestFailed(msg);
            }

        });
    }

    @Override
    public void changeFollowStatus(final User user) {
        Observable obs = RetrofitClient.getInstance().changeFollowStatus(InitManager.getInstance().getUserId(),user.userId, InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<User>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<User>() {
            @Override
            protected void onHandleSuccess(User o) {
                user.isConcerned = o.isConcerned;
                view.onChangeFollowStatusSucess(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onChangeFollowStatusFailed(msg);
            }

        });
    }
}
